<template>
  <el-dialog
    :title="!dataForm.questionId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="题库编号" prop="bankId">
      <el-input v-model="dataForm.bankId" placeholder="题库编号"></el-input>
    </el-form-item>
    <el-form-item label="题目描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="题目描述"></el-input>
    </el-form-item>
    <el-form-item label="选项(使用;分割)" prop="options">
      <el-input v-model="dataForm.options" placeholder="选项(使用;分割)"></el-input>
    </el-form-item>
    <el-form-item label="答案编号" prop="answer">
      <el-input v-model="dataForm.answer" placeholder="答案编号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          questionId: 0,
          bankId: '',
          description: '',
          options: '',
          answer: ''
        },
        dataRule: {
          bankId: [
            { required: true, message: '题库编号不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '题目描述不能为空', trigger: 'blur' }
          ],
          options: [
            { required: true, message: '选项(使用;分割)不能为空', trigger: 'blur' }
          ],
          answer: [
            { required: true, message: '答案编号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.questionId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.questionId) {
            this.$http({
              url: this.$http.adornUrl(`/wx/bankquestion/info/${this.dataForm.questionId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.bankId = data.bankQuestion.bankId
                this.dataForm.description = data.bankQuestion.description
                this.dataForm.options = data.bankQuestion.options
                this.dataForm.answer = data.bankQuestion.answer
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/wx/bankquestion/${!this.dataForm.questionId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'questionId': this.dataForm.questionId || undefined,
                'bankId': this.dataForm.bankId,
                'description': this.dataForm.description,
                'options': this.dataForm.options,
                'answer': this.dataForm.answer
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
